package com.zhaoyue.study.netty.protobuf;

import org.apache.commons.lang.time.FastDateFormat;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2017/1/16
 * Time: 16:55
 * ProtoBuf 测试类
 */
public class Test {

    public static byte[] encode(SubscribeReqProto.SubscribeReq req){
        return req.toByteArray();
    }

    public static SubscribeReqProto.SubscribeReq decode(byte[] body) throws Exception{
        return SubscribeReqProto.SubscribeReq.parseFrom(body);
    }

    public static SubscribeReqProto.SubscribeReq createSubscribeReq(){
        SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
        builder.setSubReqId(1);
        builder.setUserName("zhaoyue");
        builder.setAddress("河北武安");
        builder.setProductName("手机");
        builder.setPhoneNumber("13000000000");
        return builder.build();
    }

    public static void main(String[] args) throws Exception{

        SubscribeReqProto.SubscribeReq subscribeReq = Test.createSubscribeReq();
        System.out.println(subscribeReq.toString());
        SubscribeReqProto.SubscribeReq subscribeReq1 = decode(encode(subscribeReq));
        System.out.println("---------------");
        System.out.println(subscribeReq1.toString());
        System.out.println("---------------");
        System.out.println(subscribeReq.equals(subscribeReq1));
        System.out.println(subscribeReq == subscribeReq1);

        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        System.out.println(format.format(new Date()));
    }
}
